class code1 {
    public List<Integer> findAnagrams(String s, String p) {
        int[] hash1 = new int[26];
        int[] hash2 = new int[26];

        int left = 0;
        int right = 0;
        List<Integer> ret = new ArrayList<>();
        int len = p.length();

        //遍历p
        for(int i=0;i<len;i++) {
            hash2[p.charAt(i)-'a']++;
        }

        while( right < s.length() ) {
            //进窗口
            hash1[s.charAt(right)-'a']++;

            //判断
            while(right-left+1 > len) {
                //出窗口
                hash1[s.charAt(left)-'a']--;
                left++;
            }
            if(right-left+1 == len ) {
                if(check(hash1,hash2)) {
                    ret.add(left);
                }
            }
            right++;

        }
        return ret;
    }

    public boolean check(int[] hash1,int[] hash2) {
        for(int i=0;i< 26;i++) {
            if(hash1[i] != hash2[i]) {
                return false;
            }
        }
        return true;
    }
}